<?php

class NameLookup
{
    public static function gni($name_string)
    {
        $name_string = trim($name_string);
        if(!$name_string) return "false";
        $json = array();
        
        $url = "http://www.globalnames.org/name_strings.xml?search_term=".urlencode($name_string);
        $result = self::get_hashed_response($url);
        
        foreach($result->name_strings->name_string as $ns)
        {
            $name_string = (string) $ns->name;
            $url = "http://www.globalnames.org/name_strings?search_term=".urlencode($name_string);
            $json[] = array("name_string" => $name_string, "url" => $url, "logo_url" => WEB_ROOT . "/doc/gni_logo.png");
        }
        
        return $json;
    }
    
    public static function gni_providers($name_string)
    {
        $name_string = trim($name_string);
        if(!$name_string) return "false";
        $json = array();
        
        $url = "http://www.globalnames.org/name_strings.xml?search_term=".urlencode($name_string);
        $result = self::get_hashed_response($url);
        
        if(@$result->name_strings)
        {
            foreach($result->name_strings->name_string as $ns)
            {
                $name_string = (string) $ns->name;
                $details_url = (string) $ns->resource_uri . "?all_records=1";
                $common_name = "";
                
                $url = "http://www.globalnames.org/name_strings?search_term=".urlencode($name_string);
                $json[] = array("name_string" => $name_string, "common_name" => $common_name, "url" => $url, "source_title" => "gni", "logo_url" => WEB_ROOT . "/doc/gni_logo.png");
                
                $name_details = self::get_hashed_response($details_url);
                foreach($name_details->data->datum as $datum)
                {
                    $source_title = (string) $datum->data_source->title;
                    $logo_url = (string) $datum->data_source->logo_url;
                    if(!$logo_url) $logo_url = WEB_ROOT . "/doc/gni_logo.png";
                    
                    foreach($datum->records->record as $record)
                    {
                        $url = (string) $record->url;
                        if($url) $json[] = array("name_string" => $name_string, "common_name" => $common_name, "url" => $url, "source_title" => $source_title, "logo_url" => $logo_url);
                    }
                }
            }
        }
        
        return $json;
    }
    
    public static function eol($name_string)
    {
        $name_string = trim($name_string);
        if(!$name_string) return "false";
        $json = array();
        
        $url = "http://www.eol.org/search.xml/?q=".urlencode($name_string);
        $result = self::get_hashed_response($url);
        
        if(@$result->{'taxon-pages'}->{'taxon-page'})
        {
            foreach($result->{'taxon-pages'}->{'taxon-page'} as $page)
            {
                $name_string = (string) $page->{'canonical-form'};
                $url = "http://www.eol.org/pages/". $page->id;
                $common_name = (string) $page->{'common-name'};

                if($name_string) $json[] = array("name_string" => $name_string, "common_name" => $common_name, "url" => $url, "source_title" => "eol", "logo_url" => WEB_ROOT . "/doc/eol_logo.png");
            }
        }
        
        return $json;
    }
    
    public static function eol_images($name_string)
    {
        $name_string = trim($name_string);
        if(!$name_string) return "false";
        $json = array();
        
        $url = "http://www.eol.org/search.xml/?q=".urlencode($name_string);
        $result = self::get_hashed_response($url);
        
        foreach($result->{'taxon-pages'}->{'taxon-page'} as $page)
        {
            $name_string = (string) $page->{'canonical-form'};
            $url = "http://www.eol.org/pages/". $page->id;
            $details_url = "http://www.eol.org/pages/". $page->id . ".xml";
            
            $page_details = self::get_hashed_response($details_url);
            $image_count = (string) $page_details->images->count;
            if($image_count)
            {
                $details_url = "http://www.eol.org/pages/". $page->id . "/images/1.xml";
                $image_details = self::get_hashed_response($details_url);
                $image_url = (string) $image_details->images->image->medium_thumb_url;
                
                if($name_string && $image_url) $json[] = array("name_string" => $name_string, "image_url" => $image_url, "url" => $url, "logo_url" => WEB_ROOT . "/doc/eol_logo.png");
            }
        }
        
        return $json;
    }
    
    public static function yahoo($name_string)
    {
        $name_string = trim($name_string);
        if(!$name_string) return "false";
        
        $json = array();
        
        return $json;
    }
    
    public static function yahoo_images($name_string)
    {
        $name_string = trim($name_string);
        if(!$name_string) return "false";
        $json = array();
        
        $url = "http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=".YAHOO_API_KEY."&type=phrase&results=1&query=".urlencode($name_string);
        $result = self::get_hashed_response($url);
        
        foreach($result->Result as $image)
        {
            $name_string = $name_string;
            $url = (string) $image->RefererUrl;
            $image_url = (string) $image->Thumbnail->Url;
            
            if($name_string && $image_url) $json[] = array("name_string" => $name_string, "image_url" => $image_url, "url" => $url, "logo_url" => WEB_ROOT . "/doc/eol_logo.png");
        }
        
        return $json;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    // Needed for processing of links
    
    private static function get_hashed_response($remote_url)
    {
        $response = self::get_remote_file($remote_url);
        
        $hash = simplexml_load_string($response);
        
        return $hash;
    }
    
    private static function get_remote_file($remote_url)
    {
        $context = stream_context_create(array('http' => array('timeout' => DOWNLOAD_TIMEOUT_SECONDS)));
        
        $file = @file_get_contents($remote_url, 0, $context);
        usleep(DOWNLOAD_WAIT_TIME);
        
        $attempts = 1;
        while(!$file && $attempts < DOWNLOAD_ATTEMPTS)
        {
            $file = @file_get_contents($remote_url, 0, $context);
            usleep(DOWNLOAD_WAIT_TIME);
            $attempts++;
        }
        
        return $file;
    }
}


?>